﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using ALinq.Access;
using ALinq.Mapping;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NorthwindDemo;

namespace TestProject
{
    /// <summary>
    /// Summary description for InheritanceTest
    /// </summary>
    [TestClass]
    public class InheritanceTest
    {
        class NorthwindMappingSource : FluentMappingSource
        {
            public NorthwindMappingSource(Action<DatabaseMapping> func = null)
            {
                this.Map<NorthwindDatabase>(mapping =>
                {
                    mapping.ProviderType = typeof(AccessDbProvider);
                    mapping.Name = typeof(NorthwindDatabase).Name;
                    mapping.Table(o => o.Contacts, "Contacts", o => { o.InheritanceCode = "Unknow"; o.IsInheritanceDefault = true; })
                            .PrimaryKey(o => o.ContactID, o => { o.Storage = "_ContactID"; })
                            .Column(o => o.ContactType, o => o.IsDiscriminator = true)
                            .Column(o => o.CompanyName)
                            .Column(o => o.Phone)
                            .Column(o => o.GUID)
                            .Inheritance<FullContact>()
                            .Column(o => o.ContactName)
                            .Column(o => o.ContactTitle)
                            .Column(o => o.Address)
                            .Column(o => o.City)
                            .Column(o => o.Region)
                            .Column(o => o.PostalCode)
                            .Column(o => o.Country)
                            .Column(o => o.Fax)
                            .Inheritance<SupplierContact>()
                            .Column(o => o.HomePage)
                            .Inheritance<EmployeeContact>()
                            .Column(o => o.PhotoPath)
                            .Column(o => o.Photo)
                            .Column(o => o.Extension)
                            .Inheritance<CustomerContact>()
                            .Inheritance<ShipperContact>();

                    if (func != null)
                        func(mapping);
                });
            }
        }

        [TestMethod]
        public void Inheritance()
        {
            var mappingSource = new NorthwindMappingSource();
            var model = mappingSource.GetModel(typeof(NorthwindDatabase));
            var table = model.GetTable(typeof(Contact));
            Assert.IsNotNull(table);

            var contactType = table.RowType;
            var fullContactType = contactType.DerivedTypes[0];
            var shiperContactType = contactType.DerivedTypes[1];
            var supplierContactType = fullContactType.DerivedTypes[0];
            var employeeContactType = fullContactType.DerivedTypes[1];
            var customerContactType = fullContactType.DerivedTypes[2];

            Assert.AreEqual(typeof(Contact), contactType.Type);
            Assert.AreEqual(typeof(FullContact), fullContactType.Type);
            Assert.AreEqual(typeof(SupplierContact), supplierContactType.Type);

            //Assert.AreEqual("Full", fullContactType.InheritanceCode);
            //var subTypes = table.RowType.DerivedTypes.ToArray();
            //Assert.AreEqual(typeof(SupplierContact).FullName, subTypes[0].Name);
        }

        [TestMethod]
        public void InheritanceLoadData()
        {
            var mappingSource = new NorthwindMappingSource();
            var db = new NorthwindDatabase("C:/Northwind.mdb", mappingSource);
            var datas = db.Contacts.ToArray();
        }

        [TestMethod]
        public void GetTable()
        {
            var mappingSource = new NorthwindMappingSource(mapping =>
            {
                var contactTable = mapping.GetTable(typeof(Contact));
                Assert.IsNotNull(contactTable);

                var fullContactTable = mapping.GetTable(typeof(FullContact));
                Assert.IsNotNull(fullContactTable);
                Assert.AreEqual(contactTable, fullContactTable);

                var supplierContactTable = mapping.GetTable(typeof(ShipperContact));
                Assert.IsNotNull(supplierContactTable);
                Assert.AreEqual(supplierContactTable, fullContactTable);

                var employeeContactTable = mapping.GetTable(typeof(EmployeeContact));
                Assert.IsNotNull(employeeContactTable);
                Assert.AreEqual(supplierContactTable, employeeContactTable);
            });
        }
    }
}
